<?php

namespace mobile\controllers;

use Yii;
use common\models\meeting\Meeting;
use common\models\meeting\MeetingSearch;
use common\models\meeting\MeetingSign;
use common\models\meeting\Meetingenroll;
use common\models\AdminLog\AdminLog;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\data\Pagination;
use yii\base\Object;
use common\models\category\Category;
use yii\web\UploadedFile;
use zhongkefu\zhongkefu;
use zhongkefu\helpers\Utils;

/**
 * ArticlesController implements the CRUD actions for Articles model.
 */
class MeetingenrollController extends Controller
{

    public $layout = 'main';

    /**
     * 报名
     * $id 会议id
     */
    public function actionCreate($id)
    {

    	// 当前位置信息
    	$tid = 4;
    	$parentinfo = Category::find()->where(['status'=> 1, 'id'=> $tid])->asArray()->one();

    	// 左侧导航栏
    	$childcat = Category::find()->where(['status'=> 1, 'parent_id'=> $tid])->asArray()->all();
    	$currentid = 19;
    	$currentinfo = Category::find()->where(['status'=> 1, 'id'=> $currentid])->asArray()->one();

    	// 导航状态
    	$view = Yii::$app->view;
    	$view->params['title'] = '添加报名';
    	// 添加报名
		$res = Meeting::find()->where(['id' => $id])->one();

		$model = new Meetingenroll();
 		if($res->enroll == 1){
			if(Yii::$app->user->id){
				//会员id
				$member_id = Yii::$app->user->id;
				$ress = Meetingenroll::find()->where(['meeting_id' => $id, 'member_id' => $member_id])->asArray()->one();
				if(empty($ress)){
					if ($_POST) {
						$model->meeting_id = $id;
						$model->created_at = time();
						$model->member_id = Yii::$app->user->id;

                        $model->name = Yii::$app->request->post()['name'];
                        $model->sex = Yii::$app->request->post()['sex'];
                        $model->phone = Yii::$app->request->post()['phone'];
                        $model->email = Yii::$app->request->post()['email'];
                        $model->company = Yii::$app->request->post()['company'];
                        $model->address = Yii::$app->request->post()['address'];

                        if(!empty($_FILES['label_img']['name'])){
                                $siteRoot = Yii::$app->params['meetingimg'];
                                $json = Utils::uploadfile($_FILES['label_img']['tmp_name'],$_FILES['label_img']['name'],$siteRoot);
                                $model->label_img = $json;
                            }
						if($model->save()){
				            header("Content-type: text/html; charset=utf-8");
				            echo "<script>alert('报名成功');history.go(-1);history.go(-1);</script>";
				            exit;
						}
                        else{
                            header("Content-type: text/html; charset=utf-8");
                            echo "<script>alert('报名失败');history.go(-1);</script>";
                            exit;
                        }

					}else{
						return $this->render('create',[
							'model' => $model,
							'parentinfo' => $parentinfo,
							'currentinfo' => $currentinfo,
							'childcat' => $childcat,
						]);
					}
				} else {
		            header("Content-type: text/html; charset=utf-8");
		            echo "<script>alert('您已经报名，请勿重复报名');history.go(-1);</script>";
		            exit;
				}

			}else{
				return $this->redirect(['/auth/login']);
			}
        } else {
            return $this->render('create', [
                'model' => $model,
                'parentinfo' => $parentinfo,
				'currentinfo' => $currentinfo,
				'childcat' => $childcat,
            ]);
        }
    }
    
	//报名培训内页
    public function actionMettingview($id){
        $parenttitle = '已报名培训';
        $view = Yii::$app->view;
        $view->params['title'] = $parenttitle;
        $message = Meeting::find()->where(['id' => $id])->asArray()->one();

        $articles = Meetingenroll::find()->where(['member_id'=>\Yii::$app->user->identity->getId()])->asArray()->all();
        
        foreach($articles as $k=>$t){
            if($t['meeting_id'] == $id){
                $last = $k - 1;
                $next = $k + 1;
          } 

      }

        if(isset($articles[$last])) {
            $lastone['meeting_id'] = $articles[$last]['meeting_id'];
            $Meetings = Meeting::find()->where(['id' => $lastone['meeting_id']])->asArray()->one();
            $lastone['id'] = $Meetings['id'];
            $lastone['name'] = $Meetings['title'];
        } else {
            $lastone['id'] = $id;
            $lastone['name'] = '没有了';
        }

        if(isset($articles[$next])) {
            $nextone['meeting_id'] = $articles[$next]['meeting_id'];
            $Meetings = Meeting::find()->where(['id' => $nextone['meeting_id']])->asArray()->one();
            $nextone['id'] = $Meetings['id'];
            $nextone['name'] = $Meetings['title'];
        } else {
            $nextone['id'] = $id;
            $nextone['name'] = '没有了';
        }
         return $this->render('view',[
            'message'=>$message,
            'lastone' => $lastone,
            'nextone' => $nextone,
        ]);
    }

 	public function actionView($id)
    {

		$model = $this->findModel($id);
		Adminlog::addlog(yii::$app->user->id, "查看在线留言 ".$model->title);
        return $this->render('view', [
            'model' => $model,
        ]);
    }

    /**
     * Lists all Articles models.
     * @return mixed
     */
    public function actionList()
    {
        $parenttitle = '已报名培训';
        $view = Yii::$app->view;
        $view->params['title'] = $parenttitle;
		$id = Yii::$app->user->id;
        $data = Meeting::find()->joinWith('meetingenroll')->where(['meetingenroll.member_id'=>$id]);
        $pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '10']);
        $model = $data->offset($pages->offset)->limit($pages->limit)->asArray()->all(); //分页
        $size=10;
        if(isset($_GET['page'])){
            $page=$_GET['page'];
            $perp=$size;
        }else{
            $page=1;
            $perp=$size;
        }
        $num =$data->count();
        $cpage=ceil($num/$size);
		return $this->render('list',[
			'model' => $model,
			'page' => $pages,
            'size' => $size,
            'perp' => $perp,
            'page' => $page,
            'cpage' => $cpage,

		]);
    }
	
/* 	public function actionList(){

		$cid = Yii::$app->request->get()["cid"];
		$artice_model = Articles::find()->where(['category_id' => $cid]);
		$pages = new Pagination(['totalCount'=>$artice_model->count(), 'pageSize'=>1]);	//分页
		//动态读取内容
		$datas = $artice_model->offset($pages->offset)
		->limit($pages->limit)
		->orderby('id DESC')
		->all();
		return $this->render('list',['list' => $datas,'pages' => $pages]);
	}
 */    
	
    /**
     * Finds the Articles model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Articles the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Meeting::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
